package com.osce.score.controller;


import cn.hutool.json.JSONObject;
import com.csce.domain.entity.score.ScoreExExam;
import com.csce.domain.entity.score.excel.ExamExaminer;
import com.csce.domain.entity.score.excel.ExamSimulatedPatient;
import com.csce.domain.entity.score.excel.ExamStudent;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.interfaces.score.IScoreExExamService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考试Controller
 *
 * @author zzx
 * @since 2023-09-05
 */
@RestController
@RequestMapping("/score/score_ex_exam")
public class ScoreExExamController extends BaseController {

    @Resource
    private IScoreExExamService scoreExExamService;


    /**
     * 查询考试列表
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScoreExExam scoreExExam) {
        startPage();
        List<ScoreExExam> list = scoreExExamService.selectScoreExExamList(scoreExExam);
        return getDataTable(list);
    }

    /**
     * 导出考试列表
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:export')")
    @Log(title = "考试", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{target}")
    public void export(@PathVariable("target") String target, HttpServletResponse response, ScoreExExam scoreExExam) throws Exception {
        String name = "考试数据";
        switch (target) {
            case "all": {
                //response为HttpServletResponse对象
                //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
                response.setHeader("Content-Disposition", String.format("attachment;filename=%s.xlsx", "examDatas"));
                response.setCharacterEncoding("utf-8");
                scoreExExamService.exportExcelAllInfos(scoreExExam.getId(), response.getOutputStream());
            }break;
            case "student": {
                export(response, ExamStudent.class, scoreExExamService.getExcelStudents(scoreExExam.getId()), name);
            }break;
            case "examiner": {
                export(response, ExamExaminer.class, scoreExExamService.getExcelExaminers(scoreExExam.getId()), name);
            }break;
            case "simulated_patient": {
                export(response, ExamSimulatedPatient.class, scoreExExamService.getExcelSimulatedPatients(scoreExExam.getId()), name);
            }break;
            default: {}
        }
    }

    /**
     * 获取考试详细信息
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(scoreExExamService.selectScoreExExamById(id));
    }

    /**
     * 新增考试
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:add')")
    @Log(title = "考试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JSONObject body) {
        ScoreExExam scoreExExam = body.toBean(ScoreExExam.class);
        return toAjax(scoreExExamService.insertScoreExExam(scoreExExam)).data(scoreExExam);
    }

    /**
     * 修改考试
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:edit')")
    @Log(title = "考试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JSONObject body) {
        ScoreExExam scoreExExam = body.toBean(ScoreExExam.class);
        return toAjax(scoreExExamService.updateScoreExExam(scoreExExam)).data(scoreExExam);
    }

    /**
     * 删除考试
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:remove')")
    @Log(title = "考试", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(scoreExExamService.deleteScoreExExamByIds(ids));
    }

    /**
     * 修改考试-排站
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:edit')")
    @Log(title = "考试-排站", businessType = BusinessType.UPDATE)
    @PostMapping("/allot/{id}")
    public AjaxResult allot(@PathVariable("id") Long id) {
        return success(scoreExExamService.allot(id));
    }

    /**
     * 修改考试-清除排站
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:edit')")
    @Log(title = "考试-排站", businessType = BusinessType.UPDATE)
    @PostMapping("/clear_allot/{id}")
    public AjaxResult clearAllot(@PathVariable("id") Long id) {
        return success(scoreExExamService.clearAllot(id));
    }

    /**
     * 查询考试-排站信息
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:edit')")
    @GetMapping(value = "/allot/{id}")
    public AjaxResult getAllotInfo(@PathVariable("id") Long id) {
        return AjaxResult.success().setAll(scoreExExamService.selectAllotAll(id));
    }

    /**
     * 安排学员
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:edit')")
    @PostMapping(value = "/plan_student/{id}")
    public AjaxResult planStudent(@PathVariable("id") Long id) {
        scoreExExamService.planStudent(id);
        return AjaxResult.success();
    }

    /**
     * 重置安排学员
     */
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:edit')")
    @PostMapping(value = "/clear_plan_student/{id}")
    public AjaxResult clearPlanStudent(@PathVariable("id") Long id) {
        scoreExExamService.clearPlanStudent(id);
        return AjaxResult.success();
    }

    //-----------------------------------------------

    /**
     * 查询今天的考试列表
     */
    @GetMapping("/show/today-list")
    public AjaxResult todayList() {
        return success(scoreExExamService.selectScoreExExamTodayList());
    }

    @GetMapping("/show/get-exam-today-data/{id}")
    public AjaxResult getExamTodayDateById(@PathVariable("id") Long id) {
        return success(scoreExExamService.getExamTodayDateById(id));
    }

    /**
     * 根据考试编号获取统计数据
     */
    @GetMapping("/select-statistics/{examId}")
    @PreAuthorize("@ss.hasPermi('score:score_ex_exam:statistics')")
    public AjaxResult selectStatistics(@PathVariable("examId")Long examId){
        return success().setAll(scoreExExamService.selectStatistics(examId));
    }

}
